﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace DevEx.Thanh.Class
{
    class CUserController
    {
        QLVTDataContext objDataContext;
        public IQueryable<tblUser> SelectAllRecord()
	    {
		    try {
			    return (from u in objDataContext.tblUsers where u.IsActive == true select u);
		    } catch (Exception ex)
		    {
		        return null;
		    }
	    }
        public IQueryable<tblUser> SelectAllUserByRole(int idRole)
        {
            try
            {
                return (from u in objDataContext.tblUsers where u.IsActive == true && u.ID_Role == idRole select u);
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public IQueryable SelectAllUserByRole2(int idRole)
        {
            try
            {
                return (from u in objDataContext.tblUsers 
                        where u.IsActive == true && u.ID_Role == idRole 
                        select new {u.ID_User, u.UserName, u.FullName, u.tblKho.Kho});
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public tblUser SelectRecordByID(int _id)
	    {
		    try {
			    return (from u in objDataContext.tblUsers where u.ID_User == _id select u).Single();
		    } catch (Exception ex)
		    {
		        return null;
		    }
	    }
        public tblUser SelectUserByUserName(string sUserName)
        {
            try
            {
                return (from u in objDataContext.tblUsers where u.UserName  == sUserName && u.IsActive ==true select u).Single();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public int InsertRecord(tblUser LocalTable)
        {
            try
            {
                LocalTable.IsActive = true;
                objDataContext.tblUsers.InsertOnSubmit(LocalTable);
                objDataContext.SubmitChanges();
                return LocalTable.ID_User;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        public void UpdateRecord(tblUser LocalTable)
        {
            try
            {
                objDataContext.tblUsers.Attach(LocalTable);
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdateRecordWithoutAttach(tblUser LocalTable)
        {
            try
            {
                objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteRecord(int _id)
	    {
		    try {
			    objDataContext.tblUsers.DeleteOnSubmit(
                    (from u in objDataContext.tblUsers 
                     where u.ID_User == _id
                     select u).Single( )
                     );
			    objDataContext.SubmitChanges();
		    } catch (Exception ex) {
			    throw ex;
		    }
	    }
        public void DeleteRecord2(tblUser LocalTable)
        {
            try
            {
                objDataContext.tblUsers.Attach(LocalTable, false);
                objDataContext.tblUsers.DeleteOnSubmit(LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DeleteRecord3(tblUser LocalTable)
        {
            try
            {
                objDataContext.tblUsers.DeleteOnSubmit(LocalTable);
                objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool  CheckUserExists(string sUserName)
        {
            //cach 1
            return objDataContext.tblUsers.Any(u => u.UserName.ToLower( ) == sUserName.ToLower( ) & u.IsActive == true);
            //
        }
        public bool CheckUser(string sUseName, string sPassword)
	    {
            try
            {
                tblUser _user = (from u in objDataContext.tblUsers
                                 where u.UserName == sUseName && u.IsActive == true
                                 select u).Single();
                return (DevEx.Thanh.Class.CThanh_Utilities.verifyMd5Hash(sPassword, _user.Password));
            }
            catch (Exception)
            {return false;
            }
            return false;

	    }

        public CUserController()
        {
            objDataContext = new QLVTDataContext( );
        }
        public CUserController(QLVTDataContext  _obj)
        {
            objDataContext = _obj;
        }
    }
}
